<template>
  <!-- 添加或修改设备对话框 -->
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="show"
    @on-cancel="show = false"
    width="1200px"
    @open="onOpen"
    append-to-body
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="设备类型" prop="deviceType">
          <el-select
            v-model="queryParams.deviceType"
            placeholder="请选择设备类型"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in deviceTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item> -->

      <el-form-item label="imei" prop="imei">
        <el-input
          v-model="queryParams.imei"
          placeholder="请输入imei"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="deviceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备类型" align="left" prop="deviceType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.base_device_type"
            :value="scope.row.deviceType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="808协议终端手机号"
        align="left"
        prop="devicePhoneNum"
      />
      <el-table-column label="sim号" align="left" prop="simNum" />
      <el-table-column label="imei" align="left" prop="imei" />
      <!-- <el-table-column label="厂商" align="left" prop="manufacturer" />
      <el-table-column label="型号" align="left" prop="deviceModel" /> -->

      <el-table-column
        label="操作"
        align="left"
        class-name="small-padding fixed-width"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-paperclip"
            @click="handleBind(scope.row)"
            >绑定</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listDevice } from "@/api/base/device";
import { bindDevice } from "@/api/base/person";

export default {
  name: "Device",
  dicts: ["base_device_type"],
  props: {
    personObj: {
      type: Object,
      default: function () {
        return null;
      },
    },
    visible: {
      type: Boolean,
      default: function () {
        return false;
      },
    },
    deviceType: {
      type: [String, Number],
      default: function () {
        return "";
      },
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 设备类型字典
      deviceTypeOptions: [],
      enableOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: null,
        deviceType: null,
        bind: 0, //代表未绑定的
        imei: null,
      },
      title: "",
    };
  },
  computed: {
    show: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
  },
  created() {},
  methods: {
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.deviceType = this.deviceType;
      listDevice(this.queryParams).then((response) => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.deviceId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /**绑定按钮操作 */
    handleBind(row) {
      bindDevice({
        personId: this.personObj.personId,
        deviceId: row.deviceId,
      }).then((response) => {
        if (response.code === 200) {
          this.$modal.msgSuccess("绑定成功");
          this.getList();
          this.$emit("refreshList");
          this.show = false;
        }
      });
    },
    onOpen() {
      this.title = this.personObj.personName + "-绑定工牌";
      this.resetQuery();
    },
  },
};
</script>
